package xin.marcher.module.comment.api.impl;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import xin.marcher.module.comment.api.SkuCommentApi;
import xin.marcher.module.comment.domain.dto.AdditionalSkuCommentResultDTO;
import xin.marcher.module.comment.domain.dto.AuditResultDTO;
import xin.marcher.module.comment.domain.dto.CommentResultDTO;
import xin.marcher.module.comment.domain.dto.SkuCommentDTO;
import xin.marcher.module.comment.domain.request.*;
import xin.marcher.module.comment.service.SkuCommentService;
import xin.marcher.module.common.core.JsonResult;
import xin.marcher.module.common.core.PageResult;
import xin.marcher.module.common.exception.ProductBizException;

import javax.annotation.Resource;

/**
 * 商品评价Api
 */
@Slf4j
@DubboService(version = "1.0.0", interfaceClass = SkuCommentApi.class, retries = 0)
public class SkuCommentApiImpl implements SkuCommentApi {

    @Resource
    private SkuCommentService skuCommentService;

    /**
     * 商品评价接口
     */
    @Override
    public JsonResult<CommentResultDTO> saveComment(SkuCommentRequest request) {
        try {
            CommentResultDTO result = skuCommentService.saveComment(request);
            return JsonResult.buildSuccess(result);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    /**
     * 商品追评接口
     */
    @Override
    public JsonResult<AdditionalSkuCommentResultDTO> saveAdditionalComment(SkuAddtionalCommentRequst request) {
        try {
            AdditionalSkuCommentResultDTO result = skuCommentService.saveAddtionalComment(request);
            return JsonResult.buildSuccess(result);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    /**
     * 查询用户的商品评价列表接口
     */
    @Override
    public JsonResult<PageResult<SkuCommentDTO>> listUserCommentPage(QueryUserCommentPageRequest request) {
        try {
            PageResult<SkuCommentDTO> result = skuCommentService.listUserCommentPage(request);
            return JsonResult.buildSuccess(result);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    /**
     * 查询商品的商品评价列表接口
     */
    @Override
    public JsonResult<PageResult<SkuCommentDTO>> listSkuCommentPage(QuerySkuCommentPageRequest request) {
        try {
            PageResult<SkuCommentDTO> result = skuCommentService.listSkuCommentPage(request);
            return JsonResult.buildSuccess(result);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    /**
     * 审核初评和追评
     */
    @Override
    public JsonResult<AuditResultDTO> auditComment(AuditCommentRequest request) {
        try {
            AuditResultDTO result = skuCommentService.auditComment(request);
            return JsonResult.buildSuccess(result);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }
}
